package com.moxiao.yfjh.module.order.tradeRefund.blindTradeRefund;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.activity.common.ActivityNotifyMessageUtils;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollRespVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollUpdateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityInfo.vo.ActivityInfoUpdateReqVO;
import com.moxiao.yfjh.module.activity.convert.activityEnroll.ActivityEnrollConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityEnroll.ActivityEnrollDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.moxiao.yfjh.module.activity.service.activityEnroll.ActivityEnrollService;
import com.moxiao.yfjh.module.activity.service.activityInfo.ActivityInfoService;
import com.moxiao.yfjh.module.activity.service.activityPackage.ActivityPackageService;
import com.moxiao.yfjh.module.activity.service.activityVerificate.ActivityVerificateService;
import com.moxiao.yfjh.module.activity.service.activityenrollverificate.ActivityEnrollVerificateService;
import com.moxiao.yfjh.module.activity.service.productInfo.ProductInfoService;
import com.moxiao.yfjh.module.activity.service.productType.ProductTypeService;
import com.moxiao.yfjh.module.order.common.GenNoCommonUtil;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeRefund.vo.*;
import com.moxiao.yfjh.module.order.controller.app.vo.AppRefund;
import com.moxiao.yfjh.module.order.convert.blindTradeRefund.BlindTradeRefundConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.blindTradeRefund.BlindTradeRefundDO;
import com.moxiao.yfjh.module.order.dal.mysql.blindTradeRefund.BlindTradeRefundMapper;
import com.moxiao.yfjh.module.pay.api.refund.PayRefundApi;
import com.moxiao.yfjh.module.pay.api.refund.dto.PayRefundCreateReqDTO;
import com.moxiao.yfjh.module.pay.dal.dataobject.app.PayAppDO;
import com.moxiao.yfjh.module.pay.dal.dataobject.order.PayOrderDO;
import com.moxiao.yfjh.module.pay.service.app.PayAppService;
import com.moxiao.yfjh.module.pay.service.order.PayOrderService;
import com.moxiao.yfjh.module.pay.service.refund.PayRefundService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;
import static com.moxiao.yfjh.module.order.enums.ErrorCodeConstants.BLIND_TRADE_REFUND_NOT_EXISTS;

/**
 * 相亲订单退款 Service 实现类
 *
 * @author mx
 */
@Slf4j
@Service
@Validated
public class BlindTradeRefundServiceImpl implements BlindTradeRefundService {

    @Resource
    private BlindTradeRefundMapper blindTradeRefundMapper;
    public static final String ONE_NUMBER = "1";
    @Resource
    private ActivityEnrollService enrollService;
    @Resource
    private ActivityInfoService infoService;
    @Resource
    private ActivityEnrollVerificateService verificateService;
    @Resource
    private ActivityVerificateService activityVerificateService;
    @Resource
    private ProductTypeService productTypeService;
    @Resource
    private ActivityPackageService activityPackageService;
    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private PayRefundApi payRefundApi;
    @Resource
    private PayOrderService payOrderService;
    @Resource
    private PayAppService payAppService;
    @Resource
    private PayRefundService payRefundService;
    @Resource
    private ActivityNotifyMessageUtils messageUtils;
    @Resource
    private GenNoCommonUtil genNoCommonUtil;

    @Override

    public Long createBlindTradeRefund(BlindTradeRefundCreateReqVO createReqVO) {
        // 插入
        BlindTradeRefundDO blindTradeRefund = BlindTradeRefundConvert.INSTANCE.convert(createReqVO);

        blindTradeRefundMapper.insert(blindTradeRefund);
        // 返回
        return blindTradeRefund.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    public Long createById(BlindTradeRefundCreateReqVO createReqVO) {

        ActivityInfoDO info = infoService.getInfo(createReqVO.getOrderId());
        if (Objects.nonNull(info)) {
            // 获得个人报名信息,这里是查找已报名的
            ActivityEnrollDO enrollDO = enrollService.getSelectOne(createReqVO.getOrderId(), getLoginUserId(), (byte) 0);
            if (Objects.nonNull(enrollDO)) {

                BlindTradeRefundCreateReqVO refundCreate = new BlindTradeRefundCreateReqVO();
                // 查找未核销的可退费金额
//                refundCreate.setRefundPrice(enrollDO.getPayAmount());
                //TODO 退报名费
                BigDecimal amount = verificateService.getPrice(info.getId(), getLoginUserId(), (byte) 1, enrollDO.getId());
                refundCreate.setRefundPrice(amount);
//                if (amount.compareTo(BigDecimal.ZERO) > 0) {
//                    refundCreate.setRefundPrice(amount);
//                } else {
//                    throw exception(ENROLL_REFUND_NOT_FOUND_NOT_EXISTS);
//                }
                // 创建退款单
                refundCreate.setUserId(getLoginUserId());
                refundCreate.setActiveNo(info.getNo());
                refundCreate.setActiveName(info.getName());
                refundCreate.setOrderId(info.getId());
                refundCreate.setOrderNo(info.getNo());
                refundCreate.setApplyDescription(createReqVO.getApplyReason());
                refundCreate.setApplyPicUrls(createReqVO.getApplyPicUrls());
                refundCreate.setStatus(1);
//                String no = "RF" + "-" + NoCommonUtil.dateTime() + NoCommonUtil.getRandom();
//                refundCreate.setNo(no);
                refundCreate.setApplyReason(createReqVO.getApplyReason());
                return createBlindTradeRefund(refundCreate);
            } else {
                throw exception(ENROLL_NO_ENROLL_NOT_EXISTS);
            }
        } else {
            throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
        }
    }


    @Override
    public void updateBlindTradeRefund(BlindTradeRefundUpdateReqVO updateReqVO) {
        // 校验存在
        validateBlindTradeRefundExists(updateReqVO.getId());
        // 更新
        BlindTradeRefundDO updateObj = BlindTradeRefundConvert.INSTANCE.convert(updateReqVO);
        blindTradeRefundMapper.updateById(updateObj);
    }

    @Override
    public void deleteBlindTradeRefund(Long id) {
        // 校验存在
        validateBlindTradeRefundExists(id);
        // 删除
        blindTradeRefundMapper.deleteById(id);
    }

    private void validateBlindTradeRefundExists(Long id) {
        if (blindTradeRefundMapper.selectById(id) == null) {
            throw exception(BLIND_TRADE_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public BlindTradeRefundDO getBlindTradeRefund(Long id) {
        return blindTradeRefundMapper.selectById(id);
    }

    @Override
    public BlindTradeRefundDO getTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<BlindTradeRefundDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(BlindTradeRefundDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(BlindTradeRefundDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return this.blindTradeRefundMapper.selectOne(lqw);
    }


    @Override
    public List<BlindTradeRefundDO> getBlindTradeRefundList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return blindTradeRefundMapper.selectBatchIds(ids);
    }

    @Override
    public Integer getNo(String no) {
        return blindTradeRefundMapper.getNo(no);
    }

    @Override
    public PageResult<BlindTradeRefundDO> getBlindTradeRefundPage(BlindTradeRefundPageReqVO pageReqVO) {
        return blindTradeRefundMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<BlindTradeRefundRespVO> getJoinTradeRefundPage(BlindTradeRefundPageReqVO pageReqVO) {
        return blindTradeRefundMapper.selectJoinPage(pageReqVO);
    }


    @Override
    public List<BlindTradeRefundDO> getBlindTradeRefundList(BlindTradeRefundExportReqVO exportReqVO) {
        return blindTradeRefundMapper.selectList(exportReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean getClose(AppRefund appRefund) {
        boolean t = false;
        ActivityInfoDO info = infoService.getInfo(appRefund.getId());
        //TODO 进行中的活动 发起退款申请，进行中的活动退款，只退未核销的 只能退自己
        if (info.getStatus() == 4) {
            //TODO 进行中的活动发起退款申请
            eventInitiator(appRefund);
        } else {
            //TODO 活动发起者退全部
            if (Objects.equals(info.getHeadUserId(), getLoginUserId())) {
                // 活动发起者退款
                notStartedInitiator(appRefund);
            } else {
                //TODO 非发起者，只对自己的报名进行退费
                //TODO 对已报名的人员执行退费操作，并更改报名状态
                //TODO 获得个人报名信息，默认已报名
                notStarted(appRefund);
            }
        }
        return true;
    }

    /**
     * 已开始活动发起者
     */
    @Transactional(rollbackFor = Exception.class)
    public void eventInitiator(AppRefund appRefund) {
        ActivityInfoDO info = infoService.getInfo(appRefund.getId());
        if (Objects.nonNull(info)) {
            // 获得个人报名信息,这里是查找已报名的
            ActivityEnrollDO enrollDO = enrollService.getSelectOne(appRefund.getId(), getLoginUserId(), (byte) 0);
            if (Objects.nonNull(enrollDO)) {

                BlindTradeRefundCreateReqVO refundCreate = new BlindTradeRefundCreateReqVO();
                // 退报名费
                BigDecimal amount = verificateService.getPrice(info.getId(), getLoginUserId(), (byte) 1, enrollDO.getId());
                // 查找未核销的可退费金额
                refundCreate.setRefundPrice(amount);
//                if (amount.compareTo(BigDecimal.ZERO) > 0) {
//                    refundCreate.setRefundPrice(amount);
//                } else {
//                    throw exception(ENROLL_REFUND_NOT_FOUND_NOT_EXISTS);
//                }

                // 创建退款单
                refundCreate.setUserId(getLoginUserId());
                refundCreate.setActiveNo(info.getNo());
                refundCreate.setActiveName(info.getName());
                refundCreate.setOrderId(enrollDO.getId());
                refundCreate.setOrderNo(enrollDO.getNo());
                refundCreate.setApplyDescription(appRefund.getApplyReason());
                refundCreate.setApplyPicUrls(appRefund.getApplyPicUrls());
                refundCreate.setRefundPrice(enrollDO.getPayAmount());
                refundCreate.setStatus(1);
                genNoCommonUtil.addRefundNo(refundCreate);
                refundCreate.setApplyReason("退款申请");
                Long refundId = createBlindTradeRefund(refundCreate);
                // 创建支付退款单
                getSelf().createPayRefund(enrollDO, refundId);
                //TODO 更新报名信息
                ActivityEnrollUpdateReqVO updateReqVO = new ActivityEnrollUpdateReqVO();
                updateReqVO.setId(enrollDO.getId());
                updateReqVO.setStatus((byte) 2);
                enrollService.updateEnroll(updateReqVO);
            } else {
                throw exception(ENROLL_NO_ENROLL_NOT_EXISTS);
            }
        } else {
            throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
        }
    }

    /**
     * 相亲业务-创建退款支付单
     */
    @Transactional(rollbackFor = Exception.class)
    public void createPayRefund(ActivityEnrollDO enrollDO, long refundId) {
        log.info("活动报名退款支付开始:{}", enrollDO);
        // 创建退款单
        PayRefundCreateReqDTO createReqDTO = new PayRefundCreateReqDTO();
        // 获得相亲app的id
        PayAppDO payAppDO = payAppService.getApp(9L);
        if (Objects.nonNull(payAppDO)) {
            createReqDTO.setAppId(payAppDO.getId());
        } else {
            throw exception(PAY_REFUND_ORDER_EXISTS);
        }
        // 查找支付订单
        PayOrderDO payOrderDO = payOrderService.getPayOrder(enrollDO.getId(), 2, enrollDO.getUserId().toString());
        if (Objects.nonNull(payOrderDO)) {
            createReqDTO.setMerchantOrderId(payOrderDO.getMerchantOrderId());
            createReqDTO.setMerchantRefundId(String.valueOf(refundId));
        } else {
            throw exception(PAY_ORDER_NOT_NULL_EXISTS);
        }
        if (payOrderDO.getChannelCode().equals("mock")) {
            log.info("虚拟支付退款支付结束");
        } else {
            createReqDTO.setUserIp(enrollDO.getUserId().toString());
            createReqDTO.setReason("活动退款：退报名缴费/未核销的退款金额");
            // 创建退款单,并发起退款
            Long payRefundId = payRefundService.createPayRefund(createReqDTO);
            log.info("退款支付结束：{}", payRefundId);
        }

    }

    /**
     * 未开始活动发起者
     */

    @Transactional(rollbackFor = Exception.class)
    public void notStartedInitiator(AppRefund appRefund) {
        ActivityInfoDO info = infoService.getInfo(appRefund.getId());
        if (Objects.nonNull(info)) {
            // 关闭活动
            ActivityInfoUpdateReqVO infoUpdateReqVO = new ActivityInfoUpdateReqVO();
            infoUpdateReqVO.setId(appRefund.getId());
            infoUpdateReqVO.setStatus((byte) 7);
            infoService.updateInfo(infoUpdateReqVO);
            // 对当前活动下已报名的人员执行退费操作，并更改报名状态
            ActivityEnrollExportReqVO exportReqVO = new ActivityEnrollExportReqVO();
            exportReqVO.setActivityId(appRefund.getId());
            exportReqVO.setStatus((byte) 0);
            List<ActivityEnrollDO> enrollDOList = enrollService.getEnrollList(exportReqVO);
            if (CollectionUtils.isNotEmpty(enrollDOList)) {
                //TODO 去重复，避免出现重复退款的情况出现
//                List<ActivityEnrollDO> distinctList = enrollDOList.stream()
//                        .collect(Collectors.collectingAndThen(
//                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ActivityEnrollDO::getUserId))),
//                                ArrayList::new
//                        ));
                // 活动未开始 报名费全退，退全额
                enrollDOList.forEach(item -> {
                    //TODO 更新报名信息
                    ActivityEnrollUpdateReqVO updateReqVO = new ActivityEnrollUpdateReqVO();
                    updateReqVO.setId(item.getId());
                    updateReqVO.setStatus((byte) 2);
                    enrollService.updateEnroll(updateReqVO);
                    //TODO 添加退款记录
                    BlindTradeRefundCreateReqVO refundCreate = new BlindTradeRefundCreateReqVO();
                    refundCreate.setRefundPrice(item.getPayAmount());
                    // 查找未核销的可退费金额
//                    BigDecimal amount = verificateService.getPrice(info.getId(), item.getUserId(), (byte) 1, item.getId());
//                    if (amount.compareTo(BigDecimal.ZERO) > 0) {
//                        refundCreate.setRefundPrice(item.getPayAmount());
//                    } else {
//                        throw exception(ENROLL_REFUND_NOT_FOUND_NOT_EXISTS);
//                    }
                    refundCreate.setUserId(item.getUserId());
                    refundCreate.setActiveNo(info.getNo());
                    refundCreate.setActiveName(info.getName());
                    refundCreate.setOrderId(item.getId());
                    refundCreate.setOrderNo(item.getNo());
                    refundCreate.setApplyDescription("由活动创建者主动关闭了本次活动");
                    refundCreate.setStatus(0);
                    genNoCommonUtil.addRefundNo(refundCreate);
//                    String no = "RF" + "-" + NoCommonUtil.dateTime() + NoCommonUtil.getRandom();
//                    refundCreate.setNo(no);

                    refundCreate.setApplyReason("活动关闭");
                    Long refundId =createBlindTradeRefund(refundCreate);
                    //TODO 创建支付退款单
                    getSelf().createPayRefund(item,refundId);
                    //TODO 创建活动通知
                    ActivityEnrollRespVO enrollRespVO = ActivityEnrollConvert.INSTANCE.convert(item);
                    enrollRespVO.setActivityName(info.getName());
                    enrollRespVO.setActivityId(info.getId());
                    messageUtils.createActivityEnrollMessage(enrollRespVO);
                });
            } else {
                throw exception(ENROLL_NO_ENROLL_NOT_EXISTS);
            }
        } else {
            throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
        }
    }

    /**
     * 未开始非发起者
     */
    @Transactional(rollbackFor = Exception.class)
    public void notStarted(AppRefund appRefund) {
        ActivityInfoDO info = infoService.getInfo(appRefund.getId());
        if (Objects.nonNull(info)) {
            ActivityEnrollDO enrollDO = enrollService.getSelectOne(appRefund.getId(), getLoginUserId(), (byte) 0);
            if (Objects.nonNull(enrollDO)) {
                // 修改报名状态
                ActivityEnrollUpdateReqVO updateReqVO = new ActivityEnrollUpdateReqVO();
                updateReqVO.setId(enrollDO.getId());
                updateReqVO.setStatus((byte) 2);
                enrollService.updateEnroll(updateReqVO);
                // 查找未核销的可退费金额
//                BigDecimal amount = verificateService.getPrice(info.getId(), getLoginUserId(), (byte) 1, enrollDO.getId());
                if (enrollDO.getPayAmount().compareTo(BigDecimal.ZERO) > 0) {
                    //TODO 添加退款记录
                    BlindTradeRefundCreateReqVO refundCreate = new BlindTradeRefundCreateReqVO();
                    refundCreate.setRefundPrice(enrollDO.getPayAmount());
                    //TODO 添加退款记录
                    refundCreate.setUserId(enrollDO.getUserId());
                    refundCreate.setActiveNo(info.getNo());
                    refundCreate.setActiveName(info.getName());
                    refundCreate.setOrderId(enrollDO.getId());
                    refundCreate.setOrderNo(enrollDO.getNo());
                    refundCreate.setApplyDescription("您退出了本次活动");
                    refundCreate.setStatus(0);
                    genNoCommonUtil.addRefundNo(refundCreate);
                    refundCreate.setApplyReason("退出活动");
                    Long refundId = createBlindTradeRefund(refundCreate);
                    //TODO 创建支付退款单，并发起退款
                    getSelf().createPayRefund(enrollDO, refundId);
                    //TODO 创建活动通知
                    ActivityEnrollRespVO enrollRespVO = ActivityEnrollConvert.INSTANCE.convert(enrollDO);
                    enrollRespVO.setActivityName(info.getName());
                    enrollRespVO.setActivityId(info.getId());
                    messageUtils.createActivityEnrollMessage(enrollRespVO);
                }
                // 将未开始的活动状态修改为可报名
                if (info.getStatus() == 3) {
                    infoService.updateByStatus(info.getId(), (byte) 2);
                }
            } else {
                throw exception(ENROLL_NO_ENROLL_NOT_EXISTS);
            }
        } else {
            throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean getCloseActivityInfo(AppRefund appRefund) {
        ActivityInfoDO info = infoService.getInfo(appRefund.getId());
        //只退官方活动
        if (ONE_NUMBER.equals(info.getActiveSource())) {
            // 关闭活动
            ActivityInfoUpdateReqVO infoUpdateReqVO = new ActivityInfoUpdateReqVO();
            infoUpdateReqVO.setId(info.getId());
            infoUpdateReqVO.setStatus((byte) 7);
            infoService.updateInfo(infoUpdateReqVO);
            // 对已报名的人员执行退费操作，并更改报名状态
            ActivityEnrollExportReqVO exportReqVO = new ActivityEnrollExportReqVO();
            exportReqVO.setActivityId(info.getId());
            exportReqVO.setStatus((byte) 0);
            List<ActivityEnrollDO> enrollDOList = enrollService.getEnrollList(exportReqVO);
            if (CollectionUtils.isNotEmpty(enrollDOList)) {
//                //TODO 去重复，避免出现重复退款的情况出现
//                List<ActivityEnrollDO> distinctList = enrollDOList.stream()
//                        .collect(Collectors.collectingAndThen(
//                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ActivityEnrollDO::getUserId))),
//                                ArrayList::new
//                        ));
//                System.out.println("distinctList： " + distinctList.size());
//                System.out.println("enrollDOList： " + enrollDOList.size());
                enrollDOList.forEach(item -> {
                    // 并更改报名状态
                    ActivityEnrollUpdateReqVO updateReqVO = new ActivityEnrollUpdateReqVO();
                    updateReqVO.setId(item.getId());
                    updateReqVO.setStatus((byte) 2);
                    enrollService.updateEnroll(updateReqVO);
                    // 查找未核销的可退费金额
                    BigDecimal amount = verificateService.getPrice(info.getId(), getLoginUserId(), (byte) 1, item.getId());
                    //TODO 添加退款记录
                    BlindTradeRefundCreateReqVO refundCreate = new BlindTradeRefundCreateReqVO();
                    if (amount.compareTo(BigDecimal.ZERO) > 0) {
                        refundCreate.setRefundPrice(amount);
                    } else {
                        throw exception(ENROLL_REFUND_NOT_FOUND_NOT_EXISTS);
                    }
                    refundCreate.setUserId(item.getUserId());
                    refundCreate.setActiveNo(info.getNo());
                    refundCreate.setActiveName(info.getName());
                    refundCreate.setOrderId(item.getId());
                    refundCreate.setOrderNo(item.getNo());
                    refundCreate.setApplyDescription("平台关闭了本次活动，并进行了退款");
                    refundCreate.setRefundPrice(item.getPayAmount());
                    refundCreate.setStatus(0);
                    genNoCommonUtil.addRefundNo(refundCreate);
                    refundCreate.setApplyReason("官方活动关闭-退款");
                    Long refundId = createBlindTradeRefund(refundCreate);
                    getSelf().createPayRefund(item, refundId);

                    //TODO 创建活动通知
                    ActivityEnrollRespVO enrollRespVO = ActivityEnrollConvert.INSTANCE.convert(item);
                    enrollRespVO.setActivityName(info.getName());
                    enrollRespVO.setActivityId(info.getId());
                    messageUtils.createActivityCleanMessage(enrollRespVO);
                });
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean auditBlindTradeRefund(BlindTradeRefundUpdateReqVO vo) {
        // 1.更新活动信息
        updateBlindTradeRefund(vo);
        // 1.2 审核通过才走退款流程
        if (vo.getStatus() == 1) {
            // 2.活动信息
            ActivityInfoDO activityInfoDO = infoService.getNo(vo.getActiveNo());
            if (Objects.nonNull(activityInfoDO)) {
                // 更新活动下的人员报名信息
                ActivityEnrollDO enrollDO = enrollService.getSelectOne(activityInfoDO.getId(), vo.getUserId(), (byte) 3);
                if (Objects.nonNull(activityInfoDO)) {
                    // 更新报名状态
                    ActivityEnrollUpdateReqVO updateReqVO = new ActivityEnrollUpdateReqVO();
                    updateReqVO.setId(enrollDO.getId());
                    updateReqVO.setStatus((byte) 2);
                    enrollService.updateEnroll(updateReqVO);
                    // 调用退款支付
                    getSelf().createPayRefund(enrollDO, vo.getId());
                }
            } else {
                throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTradeRefund(Long id) {
        BlindTradeRefundDO blindTradeRefund = getBlindTradeRefund(id);
        if (Objects.nonNull(blindTradeRefund)) {
            if (blindTradeRefund.getStatus() == 1) {
                // 活动信息
                ActivityInfoDO activityInfoDO = infoService.getNo(blindTradeRefund.getActiveNo());
                if (Objects.nonNull(activityInfoDO)) {
                    // 更新活动下的人员报名信息
                    ActivityEnrollDO enrollDO = enrollService.getSelectOne(activityInfoDO.getId(), getLoginUserId(), (byte) 3);
                    if (Objects.nonNull(enrollDO)) {
                        // 更新报名状态
                        ActivityEnrollUpdateReqVO updateReqVO = new ActivityEnrollUpdateReqVO();
                        updateReqVO.setId(enrollDO.getId());
                        updateReqVO.setStatus((byte) 0);
                        enrollService.updateEnroll(updateReqVO);
                        //TODO 更新报名子表的状态,暂时屏蔽
//                        verificateService.updateBatch(enrollDO.getId(),enrollDO.getUserId() ,(byte) 0);
                    } else {
                        throw exception(ENROLL_NO_ENROLL_NOT_EXISTS);
                    }
                } else {
                    throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
                }
            } else {
                throw exception(ENROLL_REFUND_NOT_FOUND_NOT_EXISTS);
            }
        }
        BlindTradeRefundUpdateReqVO updateReqVO = new BlindTradeRefundUpdateReqVO();
        updateReqVO.setId(id);
        updateReqVO.setStatus(2);
        updateBlindTradeRefund(updateReqVO);
        return true;
    }


    /**
     * 获得自身的代理对象，解决 AOP 生效问题
     *
     * @return 自己
     */
    private BlindTradeRefundServiceImpl getSelf() {
        return SpringUtil.getBean(getClass());
    }

}
